home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byte0487.arc / GREHAN.ARC / SIEVE.ASM < prev    next >
Assembly Source File  |  1986-11-23  |  3KB  |  197 lines

  1.  
  2. ; 80386 assembly version of sieve program.
  3. ; Prints out the number of primes found in hexadecimal.
  4. ; (Printing out the number of primes found in decimal
  5. ;  is left as an exercise for the reader.)
  6. ;
  7. ; First define constants
  8. ITERVAL        equ    50    ;Iteration count
  9. TRUE        equ    1
  10. FALSE        equ    0
  11. ASIZE        equ    8190    ;Array size
  12.  
  13.         assume cs:sieve,ds:sdata
  14. sieve        segment para public use32 'code'
  15.         public _start_
  16. ;
  17. ; *** ENTRY POINT ***
  18. _start_        proc    near
  19. ;
  20. ; Tell user we are beginning the program
  21. ;
  22.         lea    ebx,startmsg
  23.         call    printmsg
  24. ;
  25. ; Setup loop counter
  26.         mov    ax,ITERVAL        ;Number of iterations
  27.         mov    iter,ax
  28. ;
  29. ; Initialize counter for number of primes
  30. L0:        xor    eax,eax
  31.         mov    count,ax
  32. ;
  33. ; Set all flags true
  34.         lea    ebx,flags
  35.         mov    ecx,ASIZE+1
  36. L1:        mov    byte ptr[ebx],TRUE
  37.         inc    ebx
  38.         loop    L1
  39. ;
  40. ; Primary loop
  41.         mov    index,eax
  42. L2:        mov    ebx,index
  43.         mov    al,flags[ebx]
  44.         or    al,al            ;Is it a prime?
  45.         jz    L5
  46. ;
  47. ; Found a prime
  48.         mov    eax,ebx            ;Twice index plus 3
  49.         add    eax,ebx
  50.         add    eax,3
  51. ;
  52. ; Kill all multiples
  53. L3:        add    ebx,eax
  54.         cmp    ebx,ASIZE
  55.         jg    L4
  56.         mov    byte ptr flags[ebx],FALSE
  57.         jmp    L3
  58. ;
  59. ; Count number of primes
  60. L4:        inc    word ptr count
  61. ;
  62. L5:        inc    dword ptr index
  63.         cmp    dword ptr index,ASIZE+1
  64.         jne    L2
  65. ;
  66. ; Do another iteration
  67.         dec    word ptr iter
  68.         jnz    L0
  69. ;
  70. ; Print out number of primes
  71. ; For now, in hex
  72.         lea    ebx,npmsg
  73.         call    printmsg
  74.         mov    ax,count
  75.         call    hwout
  76. ;
  77. ; Terminate process
  78.         mov    ax,04C00h
  79.         int    21H
  80. _start_        endp
  81. ;
  82. ; THE FOLLOWING CODE WAS INCLUDED WITH THE PHAR LAP 386ASM/
  83. ; 386LINK PACKAGE AS PART OF DEMO SOFTWARE.
  84. ;    
  85. ;    printmsg - Print a message to the screen
  86. ;
  87. ;    ebx - Points to the message to be printed out.  The message
  88. ;          must be null terminated.
  89. ;
  90.  
  91. printmsg proc    near
  92.  
  93.     push    edx            ; Save EDX.
  94.  
  95. pm1:    mov    dl,[ebx]        ; Load the next character into DL and
  96.     or    dl,dl            ;    branch if null.
  97.     je    pm3            ;
  98.  
  99.     mov    ah,02h            ; Output the character.
  100.     int    21h            ;
  101.  
  102.     cmp    byte ptr [ebx],0ah    ; If the character is a LF, then
  103.     jne    pm2            ;    also output a CR.
  104.     mov    ah,02h            ;
  105.     mov    dl,0dh            ;
  106.     int    21h            ;
  107.  
  108. pm2:    add    ebx,1            ; Increment the message pointer and
  109.     jmp    pm1            ;    loop.
  110.  
  111. pm3:    pop    edx            ; Restore EDX and return.
  112.     ret                ;
  113.  
  114. printmsg endp                ;
  115.  
  116. ;
  117. ;     hwout - Output the hex word in AX to the screen
  118. ;
  119.  
  120. hwout    proc    near            ;
  121.         
  122.     push    ax            ; Output the high byte of the word.
  123.     ror    ax,8            ;
  124.     call    hbout            ;
  125.  
  126.     pop    ax            ; Output the low byte of the word.
  127.     call    hbout            ;
  128.  
  129.     ret                ; Return.
  130.  
  131. hwout    endp                ;
  132.  
  133.  
  134. ;
  135. ;     hbout - Output the hex byte in AL to the screen
  136. ;
  137.  
  138. hbout    proc    near            ;
  139.         
  140.     push    ax            ; Output the high digit of the byte.
  141.     ror    ax,4            ;
  142.     call    hdout            ;
  143.  
  144.     pop    ax            ; Output the low digit of the byte.
  145.     call    hdout            ;
  146.  
  147.     ret                ; Return.
  148.  
  149. hbout    endp
  150.  
  151.  
  152. ;
  153. ;     hdout - Output the hex digit in AL to the screen
  154. ;
  155.  
  156. hdout    proc    near
  157.  
  158.     and    ax,0fh            ; Zap any extra bits and translate
  159.     cmp    ax,10            ;    to ASCII.
  160.     jg    hd1            ;
  161.     add    al,'0'-'A'+10        ;
  162. hd1:    add    al,'A'-10        ;
  163.  
  164.     push    dx            ; Call MS-DOS to output the digit.
  165.     mov    dl,al            ;
  166.     mov    ah,2h            ;
  167.     int    21h            ;
  168.     pop    dx            ;
  169.  
  170.     ret                ; Return.
  171.  
  172. hdout    endp
  173.  
  174.  
  175. sieve        ends
  176.  
  177.  
  178. sdata        segment para public use32 'data'
  179.  
  180. startmsg    db    'Beginning sieve.',0dh,0ah,0
  181. npmsg        db    'Number of primes:',0
  182.  
  183. flags        db    ASIZE+1 dup (?)
  184. index        dd    ?
  185. iter        dw    ?
  186. count        dw    ?
  187.  
  188. sdata        ends
  189.  
  190. _stack        segment byte stack use32 'stack'
  191.         db    8000 dup (?)
  192. _stack        ends
  193.         end
  194.  
  195.  
  196.  
  197.